import Joi from 'joi';
import { LIMITERS, FEATURES } from './constants';

const limit = Joi.object({
	limitType: Joi.string(),
	used: Joi.number().default(0).description('已用数量'),
	total: Joi.number().default(0).description('原始数量'),
	number: Joi.number().default(0).description('周期数量'),
	time: Joi.number().empty(null).description('时间段'),
});

// #region Balance
export const balance = Joi.object({
	uid: Joi.string().meta({
		_mongoose: { type: 'ObjectId', ref: 'User', index: true, unique: true },
	}),
	// 套餐ID
	planId: Joi.string()
		.allow(null)
		.meta({ _mongoose: { type: 'ObjectId', ref: 'Package' } }),
	// 套餐充值时间
	planChargeTime: Joi.date().default(Date.now),
	// 套餐过期时间
	planExpireTime: Joi.date().allow(null),
	// 功能限制
	feature: Object.keys(FEATURES).reduce((obj, x) => {
		obj[x] = Joi.boolean().default(false);
		return obj;
	}, {}),
	// 套餐使用计数
	balance: Object.keys(LIMITERS).reduce((obj, key) => {
		obj[key] = limit;
		return obj;
	}, {}),
	// 套餐异常告警计数
	planNotificationCount: Joi.object().unknown().default({}),
});

export const balanceWithId = balance.append({
	_id: Joi.string(),
});
// #endregion

// #region  Package
const pkg = Joi.object({
	// 套餐过期时间
	planExpireTime: Joi.date().allow(null),
	name: Joi.string().description('套餐名称'),
	key: Joi.string().description('英文名称	'),
	chargeType: Joi.string()
		.allow('plan', 'additionpackages')
		.description('包类型'),
	createTime: Joi.date().description('创建时间'),
	marks: Joi.string().description('备注'),
	operator: Joi.string().description('创建人'),
	packageMarks: Joi.string().description('限制套餐类型'),
	isDefaultPlan: Joi.bool().description('是否为默认套餐'),
	feature: Object.keys(FEATURES).reduce((obj, x) => {
		obj[x] = Joi.boolean().default(false);
		return obj;
	}, {}),
	limit: Object.keys(LIMITERS).reduce((obj, key) => {
		obj[key] = limit;
		return obj;
	}, {}),
});
export const packageWithId = pkg.append({
	_id: Joi.string(),
});

export { pkg as package };
// #endregion

// #region Charge Log
// export const chargeLog = Joi.object({
// 	uid: Joi.string().meta({ _mongoose: { type: 'ObjectId', ref: 'User' } }),
// 	datetime: Joi.date().default(Date.now),
// 	chargeType: Joi.string(),
// 	status: Joi.string().meta({
// 		_mongoose: { enum: ['pending', 'success', 'fail'] },
// 	}),
// });

// export const chargeLogWithId = chargeLog.append({
// 	_id: Joi.string(),
// });
// #endregion

// #region Spending Log
// export const spendingLog = Joi.object({
// 	uid: Joi.string()
// 		.meta({ _mongoose: { type: 'ObjectId', ref: 'User' } })
// 		.required(),
// 	balanceId: Joi.string().meta({
// 		_mongoose: { type: 'ObjectId', ref: 'Balance' },
// 	}),
// 	ops: Joi.array()
// 		.items(
// 			Joi.object({
// 				key: Joi.string().required(),
// 				value: Joi.number().required(),
// 			})
// 		)
// 		.required(),
// 	datetime: Joi.date().default(Date.now),
// });

// export const spendingLogWithId = spendingLog.append({
// 	_id: Joi.string(),
// });
// #endregion
